# coding=utf-8
from PyQt5 import QtCore, QtGui, QtWidgets
from main import run_run_run


class Ui_Dialog(object):
    def __init__(self):
        self.setFixedSize(567, 344)
        self.directory = ([], "")

    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.setEnabled(True)
        Dialog.resize(567, 344)
        Dialog.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
        Dialog.setMouseTracking(False)
        Dialog.setStyleSheet("font: 10pt \"华康方圆\";")

        self.lineTitle = QtWidgets.QLabel(Dialog)
        self.lineTitle.setGeometry(QtCore.QRect(175, 20, 531, 31))
        self.lineTitle.setStyleSheet("font: 14pt \"华康方圆\";")
        self.lineTitle.setObjectName("lineTitle")

        self.file_encode_label = QtWidgets.QLabel(Dialog)
        self.file_encode_label.setGeometry(QtCore.QRect(370, 80, 71, 16))
        self.file_encode_label.setStyleSheet("")
        self.file_encode_label.setObjectName("file_encode_label")

        self.encode_comboBox = QtWidgets.QComboBox(Dialog)
        self.encode_comboBox.setGeometry(QtCore.QRect(450, 70, 101, 31))
        self.encode_comboBox.setStyleSheet("font: 25 9pt \"微软雅黑\";")
        self.encode_comboBox.setObjectName("encode_comboBox")
        self.encode_comboBox.addItem("UTF-8")
        self.encode_comboBox.addItem("GBK")
        self.encode_comboBox.addItem("GB2312")
        self.encode_comboBox.addItem("ANSI")
        self.encode_comboBox.addItem("SHIFT_JIS")
        self.encode_comboBox.addItem("UTF-16")

        self.line = QtWidgets.QFrame(Dialog)
        self.line.setGeometry(QtCore.QRect(17, 110, 531, 20))
        self.line.setFrameShape(QtWidgets.QFrame.HLine)
        self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line.setObjectName("line")

        self.reg_label_1 = QtWidgets.QLabel(Dialog)
        self.reg_label_1.setGeometry(QtCore.QRect(20, 150, 21, 16))
        self.reg_label_1.setStyleSheet("")
        self.reg_label_1.setObjectName("reg_label_1")

        self.reg_edit_1 = QtWidgets.QLineEdit(Dialog)
        self.reg_edit_1.setGeometry(QtCore.QRect(50, 140, 211, 31))
        self.reg_edit_1.setStyleSheet("font: 25 9pt \"微软雅黑\";")
        self.reg_edit_1.setObjectName("reg_edit_1")

        self.rep_label_1 = QtWidgets.QLabel(Dialog)
        self.rep_label_1.setGeometry(QtCore.QRect(275, 150, 51, 16))
        self.rep_label_1.setStyleSheet("")
        self.rep_label_1.setObjectName("rep_label_1")

        self.rep_edit_1 = QtWidgets.QLineEdit(Dialog)
        self.rep_edit_1.setGeometry(QtCore.QRect(339, 140, 211, 31))
        self.rep_edit_1.setStyleSheet("font: 25 9pt \"微软雅黑\";")
        self.rep_edit_1.setObjectName("rep_edit_1")

        self.reg_label_2 = QtWidgets.QLabel(Dialog)
        self.reg_label_2.setGeometry(QtCore.QRect(20, 200, 21, 16))
        self.reg_label_2.setStyleSheet("")
        self.reg_label_2.setObjectName("reg_label_2")
        self.reg_edit_2 = QtWidgets.QLineEdit(Dialog)
        self.reg_edit_2.setGeometry(QtCore.QRect(50, 190, 211, 31))
        self.reg_edit_2.setStyleSheet("font: 25 9pt \"微软雅黑\";")
        self.reg_edit_2.setObjectName("reg_edit_2")

        self.rep_edit_2 = QtWidgets.QLineEdit(Dialog)
        self.rep_edit_2.setGeometry(QtCore.QRect(339, 190, 211, 31))
        self.rep_edit_2.setStyleSheet("font: 25 9pt \"微软雅黑\";")
        self.rep_edit_2.setObjectName("rep_edit_2")
        self.rep_label_2 = QtWidgets.QLabel(Dialog)
        self.rep_label_2.setGeometry(QtCore.QRect(275, 200, 51, 16))
        self.rep_label_2.setStyleSheet("")
        self.rep_label_2.setObjectName("rep_label_2")

        self.reg_label_3 = QtWidgets.QLabel(Dialog)
        self.reg_label_3.setGeometry(QtCore.QRect(20, 250, 21, 16))
        self.reg_label_3.setStyleSheet("")
        self.reg_label_3.setObjectName("reg_label_3")
        self.reg_edit_3 = QtWidgets.QLineEdit(Dialog)
        self.reg_edit_3.setGeometry(QtCore.QRect(50, 240, 211, 31))
        self.reg_edit_3.setStyleSheet("font: 25 9pt \"微软雅黑\";")
        self.reg_edit_3.setObjectName("reg_edit_3")
        self.rep_label_3 = QtWidgets.QLabel(Dialog)
        self.rep_label_3.setGeometry(QtCore.QRect(275, 250, 51, 16))
        self.rep_label_3.setStyleSheet("")
        self.rep_label_3.setObjectName("rep_label_3")
        self.rep_edit_3 = QtWidgets.QLineEdit(Dialog)
        self.rep_edit_3.setGeometry(QtCore.QRect(339, 240, 211, 31))
        self.rep_edit_3.setStyleSheet("font: 25 9pt \"微软雅黑\";")
        self.rep_edit_3.setObjectName("rep_edit_3")

        self.run_btn = QtWidgets.QPushButton(Dialog)
        self.run_btn.setGeometry(QtCore.QRect(110, 290, 141, 41))
        self.run_btn.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.run_btn.setMouseTracking(True)
        self.run_btn.setObjectName("run_btn")

        self.clean_btn = QtWidgets.QPushButton(Dialog)
        self.clean_btn.setGeometry(QtCore.QRect(320, 290, 141, 41))
        self.clean_btn.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.clean_btn.setMouseTracking(True)
        self.clean_btn.setObjectName("clean_btn")
        # self.file_encode_label.setBuddy(self.rep_edit_2)
        # self.rep_label_2.setBuddy(self.rep_edit_2)
        # self.reg_label_2.setBuddy(self.reg_edit_2)
        # self.reg_label_3.setBuddy(self.reg_edit_2)
        # self.rep_label_3.setBuddy(self.rep_edit_2)
        # self.reg_label_1.setBuddy(self.reg_edit_2)
        # self.rep_label_1.setBuddy(self.rep_edit_2)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

        # 文件选择
        self.file = QtWidgets.QPushButton(Dialog)
        self.file.setGeometry(QtCore.QRect(20, 70, 340, 31))
        self.file.setObjectName("file")
        self.file.setText("选择文件")
        self.file.setStyleSheet("background-color:rgb(111,180,219)")
        self.file.setStyleSheet(
            "QPushButton{background-color:rgb(111,180,219)}"  # 按键背景色
            "QPushButton:hover{color:green}"  # 光标移动到上面后的前景色
            "QPushButton{border-radius:6px}"  # 圆角半径 
            "QPushButton:pressed{background-color:rgb(180,180,180);border: None;}"  # 按下时的样式
        )
        self.file.clicked.connect(self.msg)

        self.run_btn.clicked.connect(self.run_btn_clicked)
        self.clean_btn.clicked.connect(self.clean_btn_clicked)

    # def msg(self, Filepath):
    #     m = QtWidgets.QFileDialog.getExistingDirectory(None, "选取文件夹", "./")  # 起始路径
    #     self.file.setText(m)

    def msg(self, Filepath):
        # 当窗口非继承QtWidgets.QDialog时，self需替换成 None
        directory = QtWidgets.QFileDialog.getOpenFileNames(None, None, "选取多个文件", "All Files (*);;Text Files (*.txt)",
                                                           "./")
        self.directory = directory
        self.file.setText("已选取%s个文件" % len(directory[0]))

    def run_btn_clicked(self):
        try:
            # 获取表单中填写的值
            reg_1 = self.reg_edit_1.text()
            rep_1 = self.rep_edit_1.text()
            reg_2 = self.reg_edit_2.text()
            rep_2 = self.rep_edit_2.text()
            reg_3 = self.reg_edit_3.text()
            rep_3 = self.rep_edit_3.text()
            if len(self.directory[0]) == 0:
                QtWidgets.QMessageBox.about(self, "提示", "请至少选择一个文本文件")
            else:
                file_list = self.directory[0]
                # accept_encoding = self.comboBox.currentText()
                file_encode_label = self.encode_comboBox.currentText()
                # 交由业务方法进行处理
                msg = run_run_run(file_list, file_encode_label,
                                  reg_1, rep_1, reg_2, rep_2, reg_3, rep_3)
                QtWidgets.QMessageBox.about(self, "提示", msg)
        except Exception:
            print(Exception.args)

    def clean_btn_clicked(self):
        self.reg_edit_1.clear()
        self.rep_edit_1.clear()
        self.reg_edit_2.clear()
        self.rep_edit_2.clear()
        self.reg_edit_3.clear()
        self.rep_edit_3.clear()

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "文本批量正则替换工具 - by 茵夏"))
        self.lineTitle.setText(_translate("Dialog", "文本批量正则替换工具"))
        self.file_encode_label.setText(_translate("Dialog", "文件编码"))
        self.reg_label_1.setText(_translate("Dialog", "将"))
        # self.reg_edit_1.setText('\[[\d\w" _=\.\(\),]*\]')
        self.rep_label_1.setText(_translate("Dialog", "替换为"))
        self.reg_label_2.setText(_translate("Dialog", "将"))
        self.rep_label_2.setText(_translate("Dialog", "替换为"))
        self.reg_label_3.setText(_translate("Dialog", "将"))
        self.rep_label_3.setText(_translate("Dialog", "替换为"))
        self.run_btn.setText(_translate("Dialog", "执行"))
        self.clean_btn.setText(_translate("Dialog", "清空"))
        Dialog.setToolTip(_translate("Dialog",
                                     "<html><head/><body><p><span style=\" font-weight:600;\">操作说明：</span></p><p>1、选择一个或多个文件后，选择文件编码进行操作</p><p>2、常用编码格式：中文(UTF-8),日文（SHIFT_JIS）</p><p>3、转码后统一返回中文电脑环境下可阅读的UTF-8编码</p><p>4、替换时可以输入正则表达式或者普通字符</p><p>6、生成的文件放在软件同级的display目录下</p><p>5、普通字符串用到正则表达式关键字时请进行转义</p></body></html>"))
